package br.uefs.dexa.rfframework.gui.impl.alt;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import br.uefs.dexa.rfframework.gui.impl.SimpleRFUserGUI;
import br.uefs.dexa.rfframework.gui.interfaces.RFUserGUI;
import br.uefs.dexa.rfframework.gui.utils.ScreenUtils;

public class AltSimpleRFUserGUI extends RFUserGUI {

	private static final int SCROLL_BAR_UNIT_INCREMENT = 35;
	
	
	private static final float PROGRESS_BAR_LABEL_FONT_SIZE = 18.0f;
	
	private static final int PROGRESS_BAR_WIDTH = 300;
	
	private static final int PROGRESS_BAR_HEIGTH = 30;
	
	private static AltSimpleRFUserGUI instance;

	
	
	JFrame window;
	
	//menu objects
	JMenuBar menuBar;
	JMenu fileMenu;
	JMenuItem loadImageMenuItem;

	//desktop pane
	JDesktopPane desktopPane;
	
	//internal frames
	JInternalFrame queryPatternFrame;
	JInternalFrame relevantObjectsFrame;
	JInternalFrame outputFrame;
	
	private JDialog dialog;	
	private JProgressBar progressBar;
	private Thread progressBarThread;

	
	
	private AltSimpleRFUserGUI()
	{
		super();
	}
	
	public static synchronized AltSimpleRFUserGUI getInstance()
	{
		if (instance == null) {
			try {
				EventQueue.invokeAndWait(new Runnable() {
					public void run() {
						instance = new AltSimpleRFUserGUI();
					}
				});
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		return instance;
	}
	
	@Override
	protected void compose() 
	{
		buildUi();
		window.pack();
		window.setVisible(true);
	}
	
	@Override
	protected boolean getUserSatisfaction()
	{
		return JOptionPane
				.showOptionDialog(null, "Are you satisfied?", "RFEngine",
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE, null, null,
						JOptionPane.NO_OPTION) == JOptionPane.YES_OPTION;
	}
	
	@Override
	public void startRFProgressViewer()
	{		
		progressBarThread = new Thread(new Runnable() {
			@Override
			public void run() {
				setupProgressDialog();
			}
		});
		progressBarThread.start();
		
	}
	
	private void setupProgressDialog()
	{
	
    	try {
			EventQueue.invokeAndWait(new Runnable() {
				
				@Override
				public void run() {
					
					progressBar = new JProgressBar();
					progressBar.setPreferredSize(new Dimension(PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGTH));
				    progressBar.setMaximumSize(new Dimension(PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGTH));

					Thread t = new Thread(new Runnable() {
						@Override
						public void run() {
							progressBar.setIndeterminate(true);
						}
					});
					t.start();

		    		
		    		JPanel dialogPanel = new JPanel();
		    		dialogPanel.setLayout(new BorderLayout());
		    		
		    		dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		    		JLabel label = new JLabel("retrieving...", SwingConstants.CENTER);
		    		label.setFont (label.getFont ().deriveFont (PROGRESS_BAR_LABEL_FONT_SIZE));
		    		label.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		    		dialogPanel.add(label, BorderLayout.NORTH);
		    		dialogPanel.add(progressBar, BorderLayout.SOUTH);
		         
		    		
		    		
		    		dialog = new JDialog(null, "", Dialog.ModalityType.DOCUMENT_MODAL);
		    		System.out.println("criado o dialog");
		    		dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		            
		            dialog.setModal(true);
		            dialog.add(dialogPanel);
		        	dialog.pack();
		        	dialog.setLocation((ScreenUtils.getHalfWidth() - dialog.getWidth()/2) + 35 , (ScreenUtils.getHalfHeigth() - dialog.getHeight()/2));
		        	dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
		        	dialog.setVisible(true);
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
	
	
	@Override
	public void finalizeRFProgressViewer()
	{
		hideProgressDialog();	
	}
	
	private void hideProgressDialog()
	{

		try {
			EventQueue.invokeAndWait(new Runnable() {
				public void run() {
					progressBar.setIndeterminate(false);
					dialog.setVisible(false);
					dialog.dispose();
					
					progressBarThread = null;
					dialog = null;
					progressBar = null;
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	private void buildUi() 
	{
		buildWindow();
		buildMenu();
		buildPanes();
		putEverythingTogether();
	}
	
	private void buildWindow()
	{
		window = new JFrame("RFEngine");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//window.setLayout(new BoxLayout(window.getContentPane(), BoxLayout.PAGE_AXIS));
		window.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
	
	private void buildMenu()
	{
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		loadImageMenuItem = new JMenuItem("Load Query Image");
		fileMenu.add(loadImageMenuItem);
		menuBar.add(fileMenu);
	}
	
	private void buildPanes()
	{
	
		putEverythingTogether();
	}
	
	
	private void putEverythingTogether()
	{
		window.setJMenuBar(menuBar);
		
		desktopPane = new JDesktopPane();
		
		queryPatternFrame = new JInternalFrame("Query Pattern",
				true, //resizable
				false, //closable
				true, //maximizable
				true); //iconifiable
		
		relevantObjectsFrame = new JInternalFrame("Relevant Objects",
				true, //resizable
				false, //closable
				true, //maximizable
				true); //iconifiable

		
		outputFrame = new JInternalFrame("Output",
				true, //resizable
				false, //closable
				true, //maximizable
				true); //iconifiable


		JScrollPane queryScrollPane = new JScrollPane(super.getQueryPatternPanel());
		JScrollPane relevantsScrollPane = new JScrollPane(super.getRelevantObjectsPanel());
		JScrollPane outputScrollPane = new JScrollPane(super.getObjectSetVisualizationPanel());
		
		outputScrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_BAR_UNIT_INCREMENT);
		relevantsScrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_BAR_UNIT_INCREMENT);
		queryScrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_BAR_UNIT_INCREMENT);
		
		
		
		
		window.add(desktopPane);
		
		desktopPane.add(queryPatternFrame);
		desktopPane.add(relevantObjectsFrame);
		desktopPane.add(outputFrame);
		
//		int width = (int) Math.round(  (double) ( (double)desktopPane.getSize().getWidth()/ (double)2) );
//		int heigth = (int) Math.round(  (double) ( (double)desktopPane.getSize().getHeight()/ (double)2) );
		
		int width = ScreenUtils.getHalfWidth();
		int heigth = ScreenUtils.getHalfHeigth();
		
		queryPatternFrame.setPreferredSize(new Dimension(width - 70, heigth - 50));
		queryPatternFrame.setLocation( width - 10, 10);
		queryPatternFrame.add(queryScrollPane);
		queryPatternFrame.pack();
		queryPatternFrame.setVisible(true);
		
		relevantObjectsFrame.setPreferredSize(new Dimension(width - 70, heigth - 50));
		relevantObjectsFrame.setLocation(width - 10, heigth - 20);
		relevantObjectsFrame.add(relevantsScrollPane);
		relevantObjectsFrame.pack();
		relevantObjectsFrame.setVisible(true);
		
		outputFrame.setPreferredSize(new Dimension(width - 40, (heigth * 2) - 80));
		outputFrame.setLocation(10, 10);
		outputFrame.add(outputScrollPane);
		outputFrame.pack();
		outputFrame.setVisible(true);
		
//		desktopPane.add(queryPatternFrame);
//		desktopPane.add(relevantObjectsFrame);
//		desktopPane.add(outputFrame);
//		
//		window.add(desktopPane);
		
		
	}
}
